home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / DJSRC111.ZIP / go32 / ed / unassmbl.c < prev    next >
C/C++ Source or Header  |  1993-11-22  |  29KB  |  1,116 lines

  1. /* This is file UNASSMBL.C */
  2. /*
  3. ** Copyright (C) 1993 DJ Delorie, 24 Kirsten Ave, Rochester NH 03867-2954
  4. **
  5. ** This file is distributed under the terms listed in the document
  6. ** "copying.dj", available from DJ Delorie at the address above.
  7. ** A copy of "copying.dj" should accompany this file; if not, a copy
  8. ** should be available from where this file was obtained.  This file
  9. ** may not be distributed without a verbatim copy of "copying.dj".
  10. **
  11. ** This file is distributed WITHOUT ANY WARRANTY; without even the implied
  12. ** warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. */
  14.  
  15. #include <stdio.h>
  16. #include <string.h>
  17.  
  18. #include "ed.h"
  19. #include "unassmbl.h"
  20. #include "syms.h"
  21.  
  22. #define SOURCE_LIST
  23.  
  24. int seg_size=32;
  25.  
  26. static word8 buf[20];
  27. static word32 vaddr;
  28. static int bufp, bufe;
  29. static char ubuf[100], *ubufp;
  30. static col;
  31.  
  32. static ua_str(char *s);
  33.  
  34. /* Percent tokens in strings:
  35.    First char after '%':
  36.         A - direct address
  37.         C - reg of r/m picks control register
  38.         D - reg of r/m picks debug register
  39.         E - r/m picks operand
  40.         F - flags register
  41.         G - reg of r/m picks general register
  42.         I - immediate data (takes extended size, data size)
  43.         J - relative IP offset
  44.         M - r/m picks memory
  45.         O - no r/m, offset only
  46.         R - mod of r/m picks register only
  47.         S - reg of r/m picks segment register
  48.         T - reg of r/m picks test register
  49.         X - DS:ESI
  50.         Y - ES:EDI
  51.         2 - prefix of two-byte opcode
  52.         e - put in 'e' if use32 (second char is part of reg name)
  53.             put in 'w' for use16 or 'd' for use32 (second char is 'w')
  54.         f - floating point (second char is esc value)
  55.         g - do r/m group 'n'
  56.         p - prefix
  57.         s - size override (second char is a,o)
  58.         + - make default signed
  59.    Second char after '%':
  60.         a - two words in memory (BOUND)
  61.         b - byte
  62.         c - byte or word
  63.         d - dword
  64.         p - 32 or 48 bit pointer
  65.         s - six byte pseudo-descriptor
  66.         v - word or dword
  67.         w - word
  68.         F - use floating regs in mod/rm
  69.         + - always sign
  70.         - - sign if negative
  71.         1-8 - group number, esc value, etc
  72. */
  73.  
  74. char *opmap1[] = {
  75. /* 0 */
  76.   "add %Eb,%Gb", "add %Ev,%Gv", "add %Gb,%Eb", "add %Gv,%Ev",
  77.   "add al,%I-bb", "add %eax,%I-vv", "push es", "pop es",
  78.   "or %Eb,%Gb", "or %Ev,%Gv", "or %Gb,%Eb", "or %Gv,%Ev",
  79.   "or al,%Ibb", "or %eax,%Ivv", "push cs", "%2 ",
  80. /* 1 */
  81.   "adc %Eb,%Gb", "adc %Ev,%Gv", "adc %Gb,%Eb", "adc %Gv,%Ev",
  82.   "adc al,%I-bb", "adc %eax,%I-vv", "push ss", "pop ss",
  83.   "sbb %Eb,%Gb", "sbb %Ev,%Gv", "sbb %Gb,%Eb", "sbb %Gv,%Ev",
  84.   "sbb al,%I-bb", "sbb %eax,%I-vv", "push ds", "pop ds",
  85. /* 2 */
  86.   "and %Eb,%Gb", "and %Ev,%Gv", "and %Gb,%Eb", "and %Gv,%Ev",
  87.   "and al,%Ibb", "and %eax,%Ivv", "%pe", "daa",
  88.   "sub %Eb,%Gb", "sub %Ev,%Gv", "sub %Gb,%Eb", "sub %Gv,%Ev",
  89.   "sub al,%I-bb", "sub %eax,%I-vv", "%pc", "das",
  90. /* 3 */
  91.   "xor %Eb,%Gb", "xor %Ev,%Gv", "xor %Gb,%Eb", "xor %Gv,%Ev",
  92.   "xor al,%Ibb", "xor %eax,%Ivv", "%ps", "aaa",
  93.   "cmp %Eb,%Gb", "cmp %Ev,%Gv", "cmp %Gb,%Eb", "cmp %Gv,%Ev",
  94.   "cmp al,%I-bb", "cmp %eax,%I-vv", "%pd", "aas",
  95. /* 4 */
  96.   "inc %eax", "inc %ecx", "inc %edx", "inc %ebx",
  97.   "inc %esp", "inc %ebp", "inc %esi", "inc %edi",
  98.   "dec %eax", "dec %ecx", "dec %edx", "dec %ebx",
  99.   "dec %esp", "dec %ebp", "dec %esi", "dec %edi",
  100. /* 5 */
  101.   "push %eax", "push %ecx", "push %edx", "push %ebx",
  102.   "push %esp", "push %ebp", "push %esi", "push %edi",
  103.   "pop %eax", "pop %ecx", "pop %edx", "pop %ebx",
  104.   "pop %esp", "pop %ebp", "pop %esi", "pop %edi",
  105. /* 6 */
  106.   "pusha", "popa", "bound %Gv,%Ma", "arpl %Ew,%Rw",
  107.   "%pf", "%pg", "%so", "%sa",
  108.   "push %I-vv", "imul %Gv=%Ev*%I-vv", "push %I-vb", "imul %Gv=%Ev*%I-vb",
  109.   "insb %Yb,dx", "ins%ew %Yv,dx", "outsb dx,%Xb", "outs%ew dx,%Xv",
  110. /* 7 */
  111.   "jo %Jb", "jno %Jb", "jnc %Jb", "jc %Jb",
  112.   "jz %Jb", "jnz %Jb", "jbe %Jb", "jnbe %Jb",
  113.   "js %Jb", "jns %Jb", "jpe %Jb", "jpo %Jb",
  114.   "jl %Jb", "jge %Jb", "jle %Jb", "jg %Jb",
  115. /* 8 */
  116.   "%g1 %Eb,%Ibb", "%g1 %Ev,%Ivv", "mov al,%Ibb", "%g1 %Ev,%Ivb",
  117.   "test %Eb,%Gb", "test %Ev,%Gv", "xchg %Eb,%Gb", "xchg %Ev,%Gv",
  118.   "mov %Eb,%Gb", "mov %Ev,%Gv", "mov %Gb,%Eb", "mov %Gv,%Ev",
  119.   "mov %Ew,%Sw", "lea %Gv,%M ", "mov %Sw,%Ew", "pop %Ev",
  120. /* 9 */
  121.   "nop", "xchg %eax,%ecx", "xchg %eax,%edx", "xchg %eax,%ebx",
  122.   "xchg %eax,%esp", "xchg %eax,%ebp", "xchg %eax,%esi", "xchg %eax,%edi",
  123.   "cbw", "cwd", "call %Ap", "fwait",
  124.   "push %eflags", "pop %eflags", "sahf", "lahf",
  125. /* a */
  126.   "mov al,%Ob", "mov %eax,%Ov", "mov %Ob,al", "mov %Ov,%eax",
  127.   "movsb %Xb,%Yb", "movs%ew %Xv,%Yv", "cmpsb %Xb,%Yb", "cmps%ew %Xv,%Yv",
  128.   "test al,%Ibb", "test %eax,%Ivv", "stosb %Yb,al", "stos%ew %Yv,%eax",
  129.   "lodsb al,%Xb", "lods%ew %eax,%Xv", "scasb al,%Xb", "scas%ew %eax,%Xv",
  130. /* b */
  131.   "mov al,%Ibb", "mov cl,%Ibb", "mov dl,%Ibb", "mov bl,%Ibb",
  132.   "mov ah,%Ibb", "mov ch,%Ibb", "mov dh,%Ibb", "mov bh,%Ibb",
  133.   "mov %eax,%I-vv", "mov %ecx,%I-vv", "mov %edx,%I-vv", "mov %ebx,%I-vv",
  134.   "mov %esp,%Ivv", "mov %ebp,%Ivv", "mov %esi,%I-vv", "mov %edi,%I-vv",
  135. /* c */
  136.   "%g2 %Eb,%Ibb", "%g2 %Ev,%Ibb", "ret %Iw", "ret",
  137.   "les %Gv,%Mp", "lds %Gv,%Mp", "mov %Eb,%Ibb", "mov %Ev,%I-vv",
  138.   "enter %Iww,%Ibb", "leave", "retf %Iww", "retf",
  139.   "int 3", "int %Ibb", "into", "iret",
  140. /* d */
  141.   "%g2 %Eb,1", "%g2 %Ev,1", "%g2 %Eb,cl", "%g2 %Ev,cl",
  142.   "aam", "aad", 0, "xlat",
  143.   "%f0", "%f1", "%f2", "%f3",
  144.   "%f4", "%f5", "%f6", "%f7",
  145. /* e */
  146.   "loopne %Jb", "loope %Jb", "loop %Jb", "jcxz %Jb",
  147.   "in al,%Ibb", "in %eax,%Ibb", "out %Ibb,al", "out %Ibb,%eax",
  148.   "call %Jv", "jmp %Jv", "jmp %Ap", "jmp %Jb",
  149.   "in al,dx", "in %eax,dx", "out dx,al", "out dx,%eax",
  150. /* f */
  151.   "lock %p ", 0, "repne %p ", "rep(e) %p ",
  152.   "hlt", "cmc", "%g3", "%g0",
  153.   "clc", "stc", "cli", "sti",
  154.   "cld", "std", "%g4", "%g5"
  155.   };
  156.  
  157. char *second[] = {
  158. /* 0 */
  159.   "%g6", "%g7", "lar %Gv,%Ew", "lsl %Gv,%Ew", 0, 0, "clts", 0,
  160.   0, 0, 0, 0, 0, 0, 0, 0,
  161. /* 1 */
  162.   0, 0, 0, 0, 0, 0, 0, 0,
  163.   0, 0, 0, 0, 0, 0, 0, 0,
  164. /* 2 */
  165.   "mov %Rd,%Cd", "mov %Rd,%Dd", "mov %Cd,%Rd", "mov %Dd,%Rd",
  166.   "mov %Rd,%Td", 0, "mov %Td,%Rd", 0,
  167.   0, 0, 0, 0, 0, 0, 0, 0,
  168. /* 3 */
  169.   0, 0, 0, 0, 0, 0, 0, 0,
  170.   0, 0, 0, 0, 0, 0, 0, 0,
  171.   0, 0, 0, 0, 0, 0, 0, 0,
  172.   0, 0, 0, 0, 0, 0, 0, 0,
  173.   0, 0, 0, 0, 0, 0, 0, 0,
  174.   0, 0, 0, 0, 0, 0, 0, 0,
  175.   0, 0, 0, 0, 0, 0, 0, 0,
  176.   0, 0, 0, 0, 0, 0, 0, 0,
  177.   0, 0, 0, 0, 0, 0, 0, 0,
  178.   0, 0, 0, 0, 0, 0, 0, 0,
  179. /* 8 */
  180.   "jo %Jv", "jno %Jv", "jnc %Jv", "jc %Jv",
  181.   "jz %Jv", "jnz %Jv", "jbe %Jv", "jnbe %Jv",
  182.   "js %Jv", "jns %Jv", "jpe %Jv", "jpo %Jv",
  183.   "jl %Jv", "jge %Jv", "jle %Jv", "jg %Jv",
  184. /* 9 */
  185.   "seto %Eb", "setno %Eb", "setnc %Eb", "setc %Eb",
  186.   "setz %Eb", "setnz %Eb", "setbe %Eb", "setnbe %Eb",
  187.   "sets %Eb", "setns %Eb", "setp %Eb", "setnp %Eb",
  188.   "setl %Eb", "setge %Eb", "setle %Eb", "setg %Eb",
  189. /* a */
  190.   "push fs", "pop fs", 0, "bt %Ev,%Gv",
  191.   "shld %Ev,%Gv,%Ibb", "shld %Ev,%Gv,cl", 0, 0,
  192.   "push gs", "pop gs", 0, "bts %Ev,%Gv",
  193.   "shrd %Ev,%Gv,%Ibb", "shrd %Ev,%Gv,cl", 0, "imul %Gv,%Ev",
  194. /* b */
  195.   0, 0, "lss %Mp", "btr %Ev,%Gv",
  196.   "lfs %Mp", "lgs %Mp", "movzx %Gv,%Eb", "movzx %Gv,%Ew",
  197.   0, 0, "%g8 %Ev,%Ibb", "btc %Ev,%Gv",
  198.   "bsf %Gv,%Ev", "bsr %Gv,%Ev", "movsx %Gv,%Eb", "movsx %Gv,%Ew",
  199. /* c */
  200.   0, 0, 0, 0, 0, 0, 0, 0,
  201.   0, 0, 0, 0, 0, 0, 0, 0,
  202.   0, 0, 0, 0, 0, 0, 0, 0,
  203.   0, 0, 0, 0, 0, 0, 0, 0,
  204.   0, 0, 0, 0, 0, 0, 0, 0,
  205.   0, 0, 0, 0, 0, 0, 0, 0,
  206.   0, 0, 0, 0, 0, 0, 0, 0,
  207.   0, 0, 0, 0, 0, 0, 0, 0,
  208.   };
  209.  
  210. char *groups[][8] = {     /* group 0 is group 3 for %Ev set */
  211.   { "test %Ev,%Ivv", "test %Ev,%Ivv,", "not %Ev", "neg %Ev",
  212.     "mul %eax,%Ev", "imul %eax,%Ev", "div %eax,%Ev", "idiv %eax,%Ev" },
  213.   { "add%+-", "or", "adc%+-", "sbb%+-", "and", "sub%+-", "xor", "cmp%+-" },
  214.   { "rol", "ror", "rcl", "rcr", "shl", "shr", "shl", "sar" },
  215.   { "test %Eb,%Ibb", "test %Eb,%Ibb,", "not %Eb", "neg %Eb",
  216.     "mul al,%Eb", "imul al,%Eb", "div al,%Eb", "idiv al,%Eb" },
  217.   { "inc %Eb", "dec %Eb", 0, 0, 0, 0, 0, 0 },
  218.   { "inc %Ev", "dec %Ev", "call %Ev", "call %Ep",
  219.     "jmp %Ev", "jmp %Ep", "push %Ev", 0 },
  220.   { "sldt %Ew", "str %Ew", "lldt %Ew", "ltr %Ew",
  221.     "verr %Ew", "verw %Ew", 0, 0 },
  222.   { "sgdt %Ms", "sidt %Ms", "lgdt %Ms", "lidt %Ms",
  223.     "smsw %Ew", 0, "lmsw %Ew", 0 },
  224.   { 0, 0, 0, 0, "bt", "bts", "btr", "btc" }
  225.   };
  226.  
  227. /* zero here means invalid.  If first entry starts with '*', use st(i) */
  228. /* no assumed %EFs here.  Indexed by rm(modrm()) */
  229. char *f0[] = {0, 0, 0, 0, 0, 0, 0, 0};
  230. char *fop_9[]  = { "*fxch st,%GF" };
  231. char *fop_10[] = { "fnop", 0, 0, 0, 0, 0, 0, 0 };
  232. char *fop_12[] = { "fchs", "fabs", 0, 0, "ftst", "fxam", 0, 0 };
  233. char *fop_13[] = { "fld1", "fldl2t", "fldl2e", "fldpi",
  234.                    "fldlg2", "fldln2", "fldz", 0 };
  235. char *fop_14[] = { "f2xm1", "fyl2x", "fptan", "fpatan",
  236.                    "fxtract", "fprem1", "fdecstp", "fincstp" };
  237. char *fop_15[] = { "fprem", "fyl2xp1", "fsqrt", "fsincos",
  238.                    "frndint", "fscale", "fsin", "fcos" };
  239. char *fop_21[] = { 0, "fucompp", 0, 0, 0, 0, 0, 0 };
  240. char *fop_28[] = { 0, 0, "fclex", "finit", 0, 0, 0, 0 };
  241. char *fop_32[] = { "*fadd %GF,st" };
  242. char *fop_33[] = { "*fmul %GF,st" };
  243. char *fop_36[] = { "*fsubr %GF,st" };
  244. char *fop_37[] = { "*fsub %GF,st" };
  245. char *fop_38[] = { "*fdivr %GF,st" };
  246. char *fop_39[] = { "*fdiv %GF,st" };
  247. char *fop_40[] = { "*ffree %GF" };
  248. char *fop_42[] = { "*fst %GF" };
  249. char *fop_43[] = { "*fstp %GF" };
  250. char *fop_44[] = { "*fucom %GF" };
  251. char *fop_45[] = { "*fucomp %GF" };
  252. char *fop_48[] = { "*faddp %GF,st" };
  253. char *fop_49[] = { "*fmulp %GF,st" };
  254. char *fop_51[] = { 0, "fcompp", 0, 0, 0, 0, 0, 0 };
  255. char *fop_52[] = { "*fsubrp %GF,st" };
  256. char *fop_53[] = { "*fsubp %GF,st" };
  257. char *fop_54[] = { "*fdivrp %GF,st" };
  258. char *fop_55[] = { "*fdivp %GF,st" };
  259. char *fop_60[] = { "fstsw ax", 0, 0, 0, 0, 0, 0, 0 };
  260.  
  261. char **fspecial[] = { /* 0=use st(i), 1=undefined 0 in fop_* means undefined */
  262.   0, 0, 0, 0, 0, 0, 0, 0,
  263.   0, fop_9, fop_10, 0, fop_12, fop_13, fop_14, fop_15,
  264.   f0, f0, f0, f0, f0, fop_21, f0, f0,
  265.   f0, f0, f0, f0, fop_28, f0, f0, f0,
  266.   fop_32, fop_33, f0, f0, fop_36, fop_37, fop_38, fop_39,
  267.   fop_40, f0, fop_42, fop_43, fop_44, fop_45, f0, f0,
  268.   fop_48, fop_49, f0, fop_51, fop_52, fop_53, fop_54, fop_55,
  269.   f0, f0, f0, f0, fop_60, f0, f0, f0,
  270.   };
  271.  
  272. char *floatops[] = { /* assumed " %EF" at end of each.  mod != 3 only */
  273. /*00*/ "fadd", "fmul", "fcom", "fcomp",
  274.        "fsub", "fsubr", "fdiv", "fdivr",
  275. /*08*/ "fld", 0, "fst", "fstp",
  276.        "fldenv", "fldcw", "fstenv", "fstcw",
  277. /*16*/ "fiadd", "fimul", "ficomw", "ficompw",
  278.        "fisub", "fisubr", "fidiv", "fidivr",
  279. /*24*/ "fild", 0, "fist", "fistp",
  280.        "frstor", "fldt", 0, "fstpt",
  281. /*32*/ "faddq", "fmulq", "fcomq", "fcompq",
  282.        "fsubq", "fsubrq", "fdivq", "fdivrq",
  283. /*40*/ "fldq", 0, "fstq", "fstpq",
  284.        0, 0, "fsave", "fstsww",
  285. /*48*/ "fiaddw", "fimulw", "ficomw", "ficompw",
  286.        "fisubw", "fisubrw", "fidivw", "fidivr",
  287. /*56*/ "fildw", 0, "fistw", "fistpw",
  288.        "fbldt", "fildq", "fbstpt", "fistpq"
  289.   };
  290.  
  291. static word8 getbyte(void)
  292. {
  293.   int s;
  294.   if (bufp >= bufe)
  295.   {
  296.     s = 20;
  297.     if ((vaddr & 0xfff) + s > 0x1000)
  298.       s = 0x1000 - (vaddr & 0xfff);
  299.     read_child(vaddr, buf, s);
  300.     bufe = s;
  301.     bufp = 0;
  302.   }
  303.   vaddr++;
  304.   printf("%02x", buf[bufp]);
  305.   col+=2;
  306.   return buf[bufp++];
  307. }
  308.  
  309. static int default_pick_sign;
  310.  
  311. static prefix;
  312. static modrmv;
  313. static sibv;
  314. static opsize;
  315. static addrsize;
  316.  
  317. static modrm(void)
  318. {
  319.   if (modrmv == -1)
  320.     modrmv = getbyte();
  321.   return modrmv;
  322. }
  323.  
  324. static sib(void)
  325. {
  326.   if (sibv == -1)
  327.     sibv = getbyte();
  328.   return sibv;
  329. }
  330.  
  331. #define mod(a)  (((a)>>6)&7)
  332. #define reg(a)  (((a)>>3)&7)
  333. #define rm(a)   ((a)&7)
  334. #define ss(a)   (((a)>>6)&7)
  335. #define indx(a) (((a)>>3)&7)
  336. #define base(a) ((a)&7)
  337.  
  338. /*------------------------------------------------------------------------*/
  339. uprintf(char *s, ...)
  340. {
  341.   char **a = &s;
  342.   vsprintf(ubufp, s, a+1);
  343.   while (*ubufp) ubufp++;
  344. }
  345.  
  346. uputchar(char c)
  347. {
  348.   if (c == '\t')
  349.   {
  350.     do {
  351.       *ubufp++ = ' ';
  352.     } while ((ubufp-ubuf) % 8);
  353.   }
  354.   else
  355.     *ubufp++ = c;
  356.   *ubufp = 0;
  357. }
  358.  
  359. /*------------------------------------------------------------------------*/
  360. int bytes(char c)
  361. {
  362.   switch (c)
  363.   {
  364.     case 'b':
  365.       return 1;
  366.     case 'w':
  367.       return 2;
  368.     case 'd':
  369.       return 4;
  370.     case 'v':
  371.       if (opsize == 32)
  372.         return 4;
  373.       else
  374.         return 2;
  375.   }
  376.   return 0;
  377. }
  378.  
  379. /*------------------------------------------------------------------------*/
  380. static ohex(char c, int extend, int optional, int defsize, int sign)
  381. {
  382.   static char *formats[4] = { "%#x", "%d", "%+d", "%+d" };
  383.   char *fmt;
  384.   int n=0, s=0, i, j;
  385.   int32 delta;
  386.   unsigned char buf[6];
  387.   char *name;
  388.   fmt = formats[sign];
  389.  
  390.   switch (c)
  391.   {
  392.     case 'a':
  393.       break;
  394.     case 'b':
  395.       n = 1;
  396.       break;
  397.     case 'w':
  398.       n = 2;
  399.       break;
  400.     case 'd':
  401.       n = 4;
  402.       break;
  403.     case 's':
  404.       n = 6;
  405.       break;
  406.     case 'c':
  407.     case 'v':
  408.       if (defsize == 32)
  409.         n = 4;
  410.       else
  411.         n = 2;
  412.       break;
  413.     case 'p':
  414.       if (defsize == 32)
  415.         n = 6;
  416.       else
  417.         n = 4;
  418.       s = 1;
  419.       break;
  420.     case 'x':
  421.       return;
  422.   }
  423.   for (i=0; i<n; i++)
  424.     buf[i] = getbyte();
  425.   for (; i<extend; i++)
  426.     buf[i] = (buf[i-1] & 0x80) ? 0xff : 0;
  427.   if (s)
  428.   {
  429.     uprintf("0x%02x%02x:", buf[n-1], buf[n-2]);
  430.     n -= 2;
  431.   }
  432.   switch (n)
  433.   {
  434.     case 1:
  435.       delta = *(signed char *)buf;
  436.       break;
  437.    case 2:
  438.       delta = *(signed short *)buf;
  439.       break;
  440.     case 4:
  441.       delta = *(signed long *)buf;
  442.       break;
  443.   }
  444.   if (extend > n)
  445.   {
  446.     if (delta || !optional)
  447.     {
  448.       uprintf(fmt, delta);
  449.     }
  450.     return;
  451.   }
  452.   if ((n == 4) && sign < 2)
  453.   {
  454.     name = syms_val2name(delta, &delta);
  455.     if (name)
  456.     {
  457.       uprintf("%s", name);
  458.       if (delta)
  459.         uprintf("+%lu", delta);
  460.       return;
  461.     }
  462.   }
  463.   switch (n)
  464.   {
  465.     case 1:
  466.       uprintf(fmt, (unsigned char)delta);
  467.       break;
  468.     case 2:
  469.       uprintf(fmt, (unsigned short)delta);
  470.       break;
  471.     case 4:
  472.       uprintf(fmt, (unsigned long)delta);
  473.       break;
  474.   }
  475. }
  476.  
  477. /*------------------------------------------------------------------------*/
  478.  
  479. static char *reg_names[3][8]={
  480.   "al","cl","dl","bl","ah","ch","dh","bh",
  481.   "ax","cx","dx","bx","sp","bp","si","di",
  482.   "eax","ecx","edx","ebx","esp","ebp","esi","edi" };
  483.  
  484. reg_name(int which, char size)
  485. {
  486.   if (size == 'F')
  487.   {
  488.     uprintf("st(%d)", which);
  489.     return;
  490.   }
  491.   if (((size == 'v') && (opsize == 32)) || (size == 'd'))
  492.   {
  493.     uputchar('e');
  494.   }
  495.   if (size == 'b')
  496.   {
  497.     uputchar("acdbacdb"[which]);
  498.     uputchar("llllhhhh"[which]);
  499.   }
  500.   else
  501.   {
  502.     uputchar("acdbsbsd"[which]);
  503.     uputchar("xxxxppii"[which]);
  504.   }
  505. }
  506.  
  507. /*------------------------------------------------------------------------*/
  508. do_sib(m)
  509. {
  510.   static char *i_str[] = {
  511.     "+eax", "+ecx", "+edx", "+ebx", "", "+ebp", "+esi", "+edi" };
  512.   int pick_signed = default_pick_sign;
  513.   int s, i, b, extra=0;
  514.   s = ss(sib());
  515.   i = indx(sib());
  516.   b = base(sib());
  517.   if (b == 5)
  518.   {
  519.       if (m == 0)
  520.       {
  521.         ua_str("%p:[");
  522.         ohex('d', 4, 0, addrsize, 1);
  523.       }
  524.       else
  525.       {
  526.         ua_str("%p:[ebp");
  527.         pick_signed |= 2;
  528.       }
  529.   }
  530.   else
  531.   {
  532.     static char *sib_str[] = {
  533.       "%p:[eax", "%p:[ecx", "%p:[edx", "%p:[ebx", "%p:[esp", 0, "%p:[esi", "%p:[edi" };
  534.     pick_signed |= 2;
  535.     ua_str(sib_str[b]);
  536.     if ((b == i) && (b != 4) && (i != 5))
  537.       extra = 1;
  538.   }
  539.   if (extra == 0)
  540.   {
  541.     pick_signed |= 2;
  542.     uprintf(i_str[i]);
  543.   }
  544.   if (i != 4 && s)
  545.     uprintf("*%d", (1<<s)+extra);
  546.   return pick_signed;
  547. }
  548.  
  549. /*------------------------------------------------------------------------*/
  550.  
  551. static int modrm_extend;
  552.  
  553. do_modrm(char t)
  554. {
  555.   int m = mod(modrm());
  556.   int r = rm(modrm());
  557.   int extend = (addrsize == 32) ? 4 : 2;
  558.   int pick_signed = default_pick_sign;
  559.   if (m == 3)
  560.   {
  561.     reg_name(r, t);
  562.     return;
  563.   }
  564.   if ((m == 0) && (r == 5) && (addrsize == 32))
  565.   {
  566.     ua_str("%p:[");
  567.     ohex('d', extend, 0, addrsize, 0);
  568.     uputchar(']');
  569.     return;
  570.   }
  571.   if ((m == 0) && (r == 6) && (addrsize == 16))
  572.   {
  573.     ua_str("%p:[");
  574.     ohex('w', extend, 0, addrsize, 0);
  575.     uputchar(']');
  576.     return;
  577.   }
  578.   if ((addrsize != 32) || (r != 4))
  579.     ua_str("%p:[");
  580.   if (addrsize == 16)
  581.   {
  582.     static char *r_str[] = {
  583.       "bx+si", "bx+di", "bp+si", "bp+di", "si", "di", "bp", "bx" };
  584.     uprintf(r_str[r]);
  585.     pick_signed |= 2;
  586.   }
  587.   else
  588.   {
  589.     if (r == 4)
  590.       pick_signed |= do_sib(m);
  591.     else
  592.     {
  593.       uprintf(reg_names[2][r]);
  594.       pick_signed |= 2;
  595.     }
  596.   }
  597.   modrm_extend = extend;
  598.   ohex("xbv"[m], extend, 1, addrsize, pick_signed);
  599.   uputchar(']');
  600. }
  601.  
  602. /*------------------------------------------------------------------------*/
  603. static floating_point(int e1)
  604. {
  605.   int esc = e1*8 + reg(modrm());
  606.   if (mod(modrm()) == 3)
  607.   {
  608.     if (fspecial[esc])
  609.     {
  610.       if (fspecial[esc][0] && (fspecial[esc][0][0] == '*'))
  611.       {
  612.         ua_str(fspecial[esc][0]+1);
  613.       }
  614.       else
  615.       {
  616.         ua_str(fspecial[esc][rm(modrm())]);
  617.       }
  618.     }
  619.     else
  620.     {
  621.       ua_str(floatops[esc]);
  622.       ua_str(" %EF");
  623.     }
  624.   }
  625.   else
  626.   {
  627.     ua_str(floatops[esc]);
  628.     ua_str(" %EF");
  629.   }
  630. }
  631.  
  632. /*------------------------------------------------------------------------*/
  633. static percent(char c, char **tptr)
  634. {
  635.   word32 vofs, v, delta;
  636.   char *name;
  637.   int default_signed = default_pick_sign;
  638.   char t = *(*tptr)++, it;
  639.   int extend = (addrsize == 32) ? 4 : 2;
  640.   int iextend;
  641.  
  642.   if (c != '+')
  643.   {
  644.     if (t == '-')
  645.     {
  646.       default_signed = 1;
  647.       t = *(*tptr)++;
  648.     }
  649.     else if (t == '+')
  650.     {
  651.       default_signed = 2;
  652.       t = *(*tptr)++;
  653.     }
  654.   }
  655.   switch (c)
  656.   {
  657.     case 'A':
  658.       ohex(t, extend, 0, addrsize, 0);
  659.       break;
  660.     case 'C':
  661.       uprintf("cr%d", reg(modrm()));
  662.       break;
  663.     case 'D':
  664.       uprintf("dr%d", reg(modrm()));
  665.       break;
  666.     case 'E':
  667.       do_modrm(t);
  668.       break;
  669.     case 'G':
  670.       if (t == 'F')
  671.         reg_name(rm(modrm()), t);
  672.       else
  673.         reg_name(reg(modrm()), t);
  674.       break;
  675.     case 'I':
  676.       it = *(*tptr)++;
  677.       switch (t)
  678.       {
  679.         case 'b':
  680.           iextend = 1;
  681.           break;
  682.         case 'v':
  683.           iextend = extend;
  684.           break;
  685.         default:
  686.           iextend = 0;
  687.           break;
  688.       }
  689.       ohex(it, iextend, 0, opsize, default_signed);
  690.       break;
  691.     case 'J':
  692.       switch (bytes(t))
  693.       {
  694.         case 1:
  695.           vofs = (int8)getbyte();
  696.           break;
  697.         case 2:
  698.           vofs = getbyte();
  699.           vofs += getbyte()<<8;
  700.           vofs = (int16)vofs;
  701.           break;
  702.         case 4:
  703.           vofs = (word32)getbyte();
  704.           vofs |= (word32)getbyte() << 8;
  705.           vofs |= (word32)getbyte() << 16;
  706.           vofs |= (word32)getbyte() << 24;
  707.           break;
  708.       }
  709.       name = syms_val2name(vofs+vaddr, &delta);
  710.       uprintf("%s", name);
  711.       if (delta)
  712.         uprintf("+%lu (0x%lx %c)", delta, vofs+vaddr,
  713.                 (vofs & 0x80000000L) ? 0x1e : 0x1f);
  714.       break;
  715.     case 'M':
  716.       do_modrm(t);
  717.       break;
  718.     case 'O':
  719.       ua_str("%p:[");
  720.       ohex(t, extend, 0, addrsize, 0);
  721.       uputchar(']');
  722.       break;
  723.     case 'R':
  724.       do_modrm(t);
  725.       break;
  726.     case 'S':
  727.       uputchar("ecsdfg"[reg(modrm())]);
  728.       uputchar('s');
  729.       break;
  730.     case 'T':
  731.       uprintf("tr%d", reg(modrm()));
  732.       break;
  733.     case 'X':
  734.       uprintf("ds:[");
  735.       if (addrsize == 32)
  736.         uputchar('e');
  737.       uprintf("si]");
  738.       break;
  739.     case 'Y':
  740.       uprintf("es:[");
  741.       if (addrsize == 32)
  742.         uputchar('e');
  743.       uprintf("di]");
  744.       break;
  745.     case '2':
  746.       ua_str(second[getbyte()]);
  747.       break;
  748.     case 'e':
  749.       if (opsize == 32)
  750.       {
  751.         if (t == 'w')
  752.           uputchar('d');
  753.         else
  754.         {
  755.           uputchar('e');
  756.           uputchar(t);
  757.         }
  758.       }
  759.       else
  760.         uputchar(t);
  761.       break;
  762.     case 'f':
  763.       floating_point(t-'0');
  764.       break;
  765.     case 'g':
  766.       ua_str(groups[t-'0'][reg(modrm())]);
  767.       break;
  768.     case 'p':
  769.       switch (t)
  770.       {
  771.         case 'c':
  772.         case 'd':
  773.         case 'e':
  774.         case 'f':
  775.         case 'g':
  776.         case 's':
  777.           prefix = t;
  778.           ua_str(opmap1[getbyte()]);
  779.           break;
  780.         case ':':
  781.           if (prefix)
  782.             uprintf("%cs:", prefix);
  783.           break;
  784.         case ' ':
  785.           ua_str(opmap1[getbyte()]);
  786.           break;
  787.       }
  788.       break;
  789.     case 's':
  790.       switch (t)
  791.       {
  792.         case 'a':
  793.           addrsize = 48 - addrsize;
  794.           ua_str(opmap1[getbyte()]);
  795.           break;
  796.         case 'o':
  797.           opsize = 48 - opsize;
  798.           ua_str(opmap1[getbyte()]);
  799.           break;
  800.       }
  801.       break;
  802.     case '+':
  803.       switch (t)
  804.       {
  805.         case '-':
  806.           default_pick_sign = 1;
  807.           break;
  808.         case '+':
  809.           default_pick_sign = 2;
  810.           break;
  811.         default:
  812.           default_pick_sign = 0;
  813.           break;
  814.       }
  815.   }
  816. }
  817.  
  818. static ua_str(char *s)
  819. {
  820.   int c;
  821.   if (s == 0)
  822.   {
  823.     uprintf("<invalid>");
  824.     return;
  825.   }
  826.   while ((c = *s++) != 0)
  827.   {
  828.     if (c == '%')
  829.     {
  830.       c = *s++;
  831.       percent(c, &s);
  832.     }
  833.     else
  834.       if (c == ' ')
  835.         uputchar('\t');
  836.       else
  837.         uputchar(c);
  838.   }
  839. }
  840.  
  841. #ifdef SOURCE_LIST
  842. /*
  843. ** A little brute force hacking and hey presto! A source debugger!
  844. ** Courtesy of Kent Williams williams@herky.cs.uiowa.edu
  845. **
  846. ** KNOWN BUGS:
  847. ** The program will summarily terminate if you run out
  848. ** of memory while you're looking for all the line offsets.  Since
  849. ** a two thousand line source file only creats an 8K array, and the
  850. ** symbol table goes into virtual memory, this shouldn't happen too
  851. ** often.
  852. **
  853. ** One file is left open for reading indefinitely.
  854. */
  855. #include <stdlib.h>
  856. #include <string.h>
  857. /*
  858. ** keep the source line offsets in virtual memory, so you can
  859. ** debug big programs
  860. */
  861. extern word32 salloc(word32 size);
  862. #define symsput(where,ptr,size)     memput(where,ptr,size)
  863. #define symsget(where,ptr,size)     memget(where,ptr,size)
  864.  
  865. /*
  866. ** for each file encountered, keep an array of line start offsets
  867. ** so you can seek into the file to display the current line.
  868. */
  869. typedef struct {
  870.         char *filename;
  871.         long *offsets;
  872. } line_info;
  873.  
  874. static line_info *files;
  875. static last_file = 0;
  876.  
  877. /*
  878. ** add_file -- add a file to the source line database
  879. */
  880. static int
  881. add_file(char *name) {
  882.         FILE *f = fopen(name,"rb");
  883.         char c;
  884.         long *lines,curpos;
  885.         unsigned curline = 0;
  886.  
  887.         if(!f)
  888.                 return -1;
  889.  
  890.         if (files == 0)
  891.           files = (line_info *)malloc(sizeof(line_info));
  892.         else
  893.           files = realloc(files, (last_file+1) * sizeof(line_info));
  894.  
  895.         files[last_file].filename = strdup(name);
  896.  
  897.         /*
  898.         ** build an array of line offsets in real memory.
  899.         */
  900.         lines = malloc(sizeof(long));
  901.         lines[curline++] = curpos = 0L;
  902.  
  903.         while((c = getc(f)) != EOF) {
  904.                 curpos++;
  905.                 if(c == '\n') {
  906.                         lines = realloc(lines,sizeof(long)*(curline+1));
  907.                         lines[curline++] = curpos;
  908.                 }
  909.         }
  910.         /*
  911.         ** now move the whole array into virtual memory
  912.         */
  913.         files[last_file].offsets = lines;
  914.         fclose(f);
  915.  
  916.         last_file++;
  917.         return 0;
  918. }
  919.  
  920. static line_info *
  921. find_file(char *name) {
  922.         int i;
  923.         for(i = 0; i < last_file; i++)
  924.                 if(strcmp(name,files[i].filename) == 0)
  925.                         return &files[i];
  926.         if(add_file(name) == -1)
  927.                         return NULL;
  928.         return find_file(name);
  929. }
  930.  
  931. /*
  932. ** myfopen -- cache the most recently accessed source file
  933. ** so you aren't constantly reopening a new file
  934. */
  935. static FILE *
  936. myfopen(char *name) {
  937.         static char fname[80] = "";
  938.         static FILE *current = NULL;
  939.         if(current != NULL && strcmp(fname,name) == 0)
  940.                         return current;
  941.         if(current != NULL) fclose(current);
  942.         strcpy(fname,name);
  943.         return (current = fopen(name,"rb"));
  944. }
  945.  
  946. /*
  947. ** put_source_line -- print the current source line, along with
  948. ** the line # and file name, if necessary.
  949. */
  950. void
  951. put_source_line(int fmt,char *name,int line) {
  952.         line_info *current = find_file(name);
  953.         FILE *cur;
  954.         if(current == NULL) {
  955.         regular:
  956.           if(fmt == 0)
  957.                   printf(" (%s#%d):\n", name, line);
  958.           else
  959.                   printf("#%d:\n", line);
  960.         } else {
  961.                 char buf[70];
  962.                 long offset;
  963.                 if((cur = myfopen(name)) == NULL)
  964.                         goto regular;
  965.                 /*
  966.                 ** get the symbol out of virtual memory
  967.                 */
  968.                 offset = current->offsets[line-1];
  969.                 fseek(cur,offset,0);
  970.                 /*
  971.                 ** truncate line so it fits on screen.
  972.                 */
  973.                 fgets(buf,sizeof(buf)-2,cur);
  974.                 if(strchr(buf,'\n') == NULL)
  975.                         strcat(buf,"\n");
  976.                 if(fmt == 0)
  977.                         printf(" (%s#%d): %s", name, line,buf);
  978.                 else
  979.                         printf("#%d: %s",line,buf);
  980.         }
  981. }
  982.  
  983. #endif
  984.  
  985.  
  986. int last_unassemble_unconditional;
  987. int last_unassemble_jump;
  988. int last_unassemble_extra_lines;
  989.  
  990. word32 unassemble(word32 v, int showregs)
  991. {
  992.   int a,b,n,wi, linenum;
  993.   char *cmp, *brp;
  994.   word8 *wp;
  995.   word32 delta;
  996.   char *name, *lname;
  997.  
  998.   default_pick_sign = 0;
  999.   ansi(A_yellow);
  1000.   last_unassemble_unconditional = 0;
  1001.   last_unassemble_jump = 0;
  1002.   last_unassemble_extra_lines = 0;
  1003.   name = syms_val2name(v, &delta);
  1004.   if (!delta && (name[0] != '0'))
  1005.   {
  1006.     printf("%s()", name);
  1007.     lname = syms_val2line(v, &linenum, 1);
  1008.     if (lname)
  1009. #ifndef SOURCE_LIST
  1010.       printf(" (%s#%d):\n", lname, linenum);
  1011. #else
  1012.       put_source_line(0,lname,linenum);
  1013. #endif
  1014.     else
  1015.       printf(":\n");
  1016.     last_unassemble_extra_lines++;
  1017.   }
  1018.   else
  1019.   {
  1020.     lname = syms_val2line(v, &linenum, 1);
  1021.     if (lname)
  1022.     {
  1023. #ifndef SOURCE_LIST
  1024.       printf("#%d:\n", linenum);
  1025. #else
  1026.       put_source_line(1,lname,linenum);
  1027. #endif
  1028.       last_unassemble_extra_lines++;
  1029.     }
  1030.   }
  1031.  
  1032.   ansi(A_grey);
  1033.   printf("%08lx: ", v);
  1034. #if 0
  1035.   if (!page_is_valid(v+ARENA) || !page_is_valid(v+5+ARENA))
  1036.   {
  1037.     printf("<bad address>\n");
  1038.     return v;
  1039.   }
  1040. #endif
  1041.  
  1042.   prefix = 0;
  1043.   modrmv = sibv = -1;
  1044.   opsize = addrsize = seg_size;
  1045.   vaddr = v;
  1046.   bufp = bufe = 0;
  1047.   col = 0;
  1048.   ubufp = ubuf;
  1049.   ua_str(opmap1[getbyte()]);
  1050.   do {
  1051.     putchar(' ');
  1052.     col++;
  1053.   } while (col < 15);
  1054.   col += strlen(ubuf);
  1055.   do {
  1056.     uputchar(' ');
  1057.     col++;
  1058.   } while (col < 43);
  1059.  
  1060.   ansi(A_cyan);
  1061.   printf("%s", ubuf);
  1062.   ansi(A_grey);
  1063.  
  1064.   if ((strncmp(ubuf, "jmp ", 4) == 0)
  1065.      || (strncmp(ubuf, "ret", 3) == 0))
  1066.     last_unassemble_unconditional = 1;
  1067.   if (ubuf[0] == 'j')
  1068.     last_unassemble_jump = 1;
  1069.  
  1070.   if (!showregs)
  1071.   {
  1072.     putchar('\n');
  1073.     return vaddr;
  1074.   }
  1075.  
  1076.   col -= 43; /* total 25 columns left */
  1077.   wp = (word8 *)&(a_tss.tss_eax);
  1078.   cmp = strchr(ubuf+8, ',');
  1079.   brp = strchr(ubuf+8, '[');
  1080.   if (!cmp) cmp = ubuf+8;
  1081.   if (!brp) brp = ubufp;
  1082.   if (brp < cmp) cmp = brp;
  1083.   if (strncmp(ubuf, "mov ", 4))
  1084.     cmp = ubuf+8;
  1085.   for (b=0; b<8; b++)
  1086.   {
  1087.     for (a=2; a>=0; a--)
  1088.     {
  1089.       n = (a==0) ? 1 : ((a==1) ? 2 : 4);
  1090.       if (strstr(cmp, reg_names[a][b]))
  1091.       {
  1092.         col += strlen(reg_names[a][b])+n*2+2;
  1093.         if (col > 29)
  1094.         {
  1095.           printf("\n%53s", "");
  1096.           col = 0;
  1097.         }
  1098.         printf("%s=", reg_names[a][b]);
  1099.         if (a == 0)
  1100.           wi = (b&3)*4 + (b>>2);
  1101.         else
  1102.           wi = b*4;
  1103.         while (n)
  1104.         {
  1105.           n--;
  1106.           printf("%02x", wp[wi+n]);
  1107.         }
  1108.         putchar(' ');
  1109.         break;
  1110.       }
  1111.     }
  1112.   }
  1113.   putchar('\n');
  1114.   return vaddr;
  1115. }
  1116.